﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyExe.Help
{
    /// <summary>
    /// 
    /// </summary>
    public class FileSearch
    {
        /// <summary>
        /// 查找文件夹下的文件
        /// </summary>
        /// <param name="folderPath"></param>
        /// <param name="fileQueue"></param>
        /// <param name="fileCountLength"></param>
        /// <param name="extensions"></param>
        public static void GetFiles(string folderPath, ref ConcurrentQueue<string> fileQueue, ref long fileCountLength, List<string> extensions)
        {
            try
            {
                if (string.IsNullOrEmpty(folderPath))
                    return;
                DirectoryInfo directory = new DirectoryInfo(folderPath);
                if (directory.Exists)
                {
                    foreach (FileInfo info in directory.GetFiles())
                    {
                        if (extensions.Contains(info.Extension.ToLower()))
                        {
                            fileCountLength += info.Length;
                            if (!fileQueue.Any(p => p == info.FullName))
                            {
                                fileQueue.Enqueue(info.FullName);
                            }
                        }
                    }
                    foreach (DirectoryInfo info in directory.GetDirectories())
                    {
                        GetFiles(info.FullName, ref fileQueue, ref fileCountLength, extensions);
                    }
                }
            }
            catch
            {
            }
        }
    }
}
